package com.zcx.qshop.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zcx.helper.db.SQLiteTable;
import com.zcx.helper.db.note.SQLiteTableInfo;
import com.zcx.qshop.conn.JsonHotwordsAsyGet;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/8/27 0027.
 */
@SQLiteTableInfo(name = "search", structure = {"title"})
public class SearchTable extends SQLiteTable {

    private  SQLiteDatabase db;

    public void insert(String title){

        if(!isHas(title)){

            db = dataBase.getWritableDatabase();

            ContentValues contentValues = new ContentValues();

            contentValues.put("title", title);

            db.insert("search", null, contentValues);

        }

    }

    public List<JsonHotwordsAsyGet.Info.Word> queryAll(){

        List<JsonHotwordsAsyGet.Info.Word> list = new ArrayList<>();

        db = dataBase.getReadableDatabase();

        Cursor cursor = db.query("search", new String[]{"_id", "title"}, null, null, null, null, "_id desc");

        while (cursor.moveToNext()) {

            JsonHotwordsAsyGet.Info.Word word = new JsonHotwordsAsyGet.Info.Word();

            word.title = cursor.getString(1);

            list.add(word);

        }

        return list;

    }

    public boolean isHas(String title){

        db = dataBase.getReadableDatabase();

        Cursor cursor = db.query("search", new String[]{"_id", "title"}, "title=?", new String[]{ title }, null, null, null);

        int count = cursor.getCount();

        cursor.close();

        db.close();

        return count > 0;

    }

    public void deleteAll(){

        db = dataBase.getWritableDatabase();

        db.delete("search", null, null);

        db.close();

    }

}
